<?php
class InternshipsController extends AppController {

	var $name = 'Internships';

	function index() {
		if($this->Session->Read('user.level') == '0'){
			//TODO codigo para o aluno
			//pr($this->Session->Read('user'));
			$uId = $this->Session->Read('user.id');
			$this->loadModel('Student');
			$std = $this->Student->findById($uId);
			$this->set('std', $std);
		} else if($this->Session->Read('user.level') == '1') {
			//TODO codigo para o administrador
			$this->Internship->recursive = 0;
			$this->set('internships', $this->paginate());
		} else $this->redirect(array('controller' => 'users', 'action'=>'home'));
	}

	
	
	function beggin(){
		$this->_onlyToLogged();
		$uId = $this->Session->Read('user.id');
		$this->loadModel('Student');
		$this->loadModel('Company');
		$std = $this->Student->findById($uId);
		$this->set('std', $std);
		$int = $this->Internship->findByStudents_id($std['Student']['id']);
		if($std){
			$this->set('int', $int);
			// Quando não existir Internship. Cria-se um
			if(!$int){
				// Quando preencher o formulario
				if($this->data) $this->__saveInternship($this->data);
				else{
					// Monta o select de Supervisors
					$this->loadModel('Supervisor');
					$supervisors = $this->Supervisor->find('all');
					$selectSup = array(""=>"");
					foreach ($supervisors as $sup){
						$selectSup[$sup['Supervisor']['id']] = $sup['Supervisor']['name'];
					}
					//Cria a variável na sessão para a View
					$this->set('selectSup', $selectSup);
					
					// Monta o select de Companies
					$companies = $this->Company->find('all');
					$selectComp = array(""=>"");
					foreach($companies as $comp){
						$selectComp[$comp["Company"]["id"]] = $comp["Company"]["social_reason"]." - cnpj: ".$comp["Company"]["cnpj"];
					}
					//Cria a variável para a View
					$this->set('selectComp', $selectComp);
				}
			} else {
				$comp = $this->Company->findById($int['Internship']['companies_id']);
				$docs = array();
				if($int['Internship']['type'] == '1' && $int['Internship']['program'] == '0')
					$docs[] = "Internship Program";
				if($int['Internship']['siga_registration'] == '0')
					$docs[] = "Siga Registration";
				if($int['Internship']['charter_request'] == '0')
					$docs[] = "Charter Request";
				if($int['Internship']['term_commitment'] == '0')
					$docs[] = "Term Commitment";
				if($comp['Company']['certificate_suitability'] == '0')
					$docs[] = "Certificate of Suitability";
				$this->set('docs', $docs);
			}
		}
	}
	
	private function __saveInternship($data){
		$data['Internship']['students_id'] = $this->Session->Read('user.id');
		pr($data);
		$this->Internship->create();
		if ($this->Internship->save($data)) {
			$this->Session->setFlash(__('The internship had been saved', true));
			$this->redirect(array('controller'=>'users', 'action'=>'home'));
		} else {
			$this->Session->setFlash(__('The internship could not be saved. Please, try again.', true));
		}
		
	}
	
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid internship', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('internship', $this->Internship->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Internship->create();
			if ($this->Internship->save($this->data)) {
				$this->Session->setFlash(__('The internship had been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The internship could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid internship', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Internship->save($this->data)) {
				$this->Session->setFlash(__('The internship has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The internship could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Internship->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for internship', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Internship->delete($id)) {
			$this->Session->setFlash(__('Internship deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Internship was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	private function _onlyToAdmin(){
		$this->_onlyToLogged();
		if($this->Session->Read('user.level') != '1')
			$this->redirect(array('controller'=>'users', 'action'=>'home'));
	}
	
	private function _onlyToLogged(){
		if($this->Session->Read('user.email') == '')
			$this->redirect(array('controller'=>'users', 'action'=>'home'));
	}
	
	
}
?>